package com.bfly.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bfly.common.json.JsonUtil;
import com.bfly.core.base.entity.RemoteResponse;
import com.bfly.core.cache.SysConfigCacheUtil;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.system.dto.SystemConfigDTO;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * 腾讯地图工具类
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/11/9 19:42
 */
public class TenCentMapUtil {

    private static final String TENCENT_MAP_GEOCODER_URL = "https://apis.map.qq.com/ws/geocoder/v1/";
    private static final String TENCENT_DISTANCE_URL = "http://apis.map.qq.com/ws/distance/v1/matrix/";

    /**
     * 组装参数
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/6/9 22:32
     */
    private static Map<String, Object> getTenCentParamMap() {
        SystemConfigDTO systemConfig = SysConfigCacheUtil.getSystemConfig();
        Map<String, Object> param = new HashMap<>(2);
        param.put("key", systemConfig.getTenCentMapKey());
        return param;
    }

    /**
     * 根据坐标获得位置信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/9 19:12
     */
    public static JSONObject getAddress(@RequestParam("latitude") String latitude, @RequestParam("longitude") String longitude) throws Exception {
        Map<String, Object> param = getTenCentParamMap();
        param.put("location", latitude + "," + longitude);
        RemoteResponse response = NetUtil.get(TENCENT_MAP_GEOCODER_URL, param);
        JSONObject json = JsonUtil.parse(response.getBody(), JSONObject.class);
        if (json.getInteger("status") != 0) {
            throw new ServiceResponseException(SysError.ERROR, json.getString("message"));
        }
        // 请求成功
        json = json.getJSONObject("result");

        JSONObject location = new JSONObject();
        location.put("latitude", latitude);
        location.put("longitude", longitude);
        location.put("address", json.getString("address"));

        json = json.getJSONObject("address_component");
        location.put("nation", json.getString("nation"));
        location.put("province", json.getString("province"));
        location.put("city", json.getString("city"));
        location.put("district", json.getString("district"));
        location.put("street", json.getString("street"));
        location.put("street_number", json.getString("street_number"));
        return location;
    }

    /**
     * 根据地址名称获得坐标
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/29 20:07
     */
    public static JSONObject geoCoder(String address) {
        Map<String, Object> param = getTenCentParamMap();
        param.put("address", address);
        RemoteResponse response = NetUtil.get(TENCENT_MAP_GEOCODER_URL, param);
        JSONObject json = JsonUtil.parse(response.getBody(), JSONObject.class);
        if (json.getInteger("status") != 0) {
            throw new ServiceResponseException(SysError.ERROR, json.getString("message"));
        }
        // 请求成功
        json = json.getJSONObject("result");
        json = json.getJSONObject("location");
        return json;
    }

    /**
     * 驾车模式:起始坐标到终止坐标测距
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/6/9 22:28
     */
    public static JSONArray distance(String from, String to) throws Exception {
        Map<String, Object> param = getTenCentParamMap();
        // 驾车模式
        param.put("mode", "driving");
        param.put("from", from);
        param.put("to", to);
        RemoteResponse response = NetUtil.get(TENCENT_DISTANCE_URL, param);
        JSONObject json = JsonUtil.parse(response.getBody(), JSONObject.class);
        if (json.getInteger("status") != 0) {
            throw new ServiceResponseException(SysError.ERROR, json.getString("message"));
        }
        // 请求成功
        json = json.getJSONObject("result");
        JSONArray array = json.getJSONArray("rows");
        return array;
    }
}
